package com.todod.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.entity.ResultData;
import com.todod.mapper.ProjectCatalogMapper;
import com.todod.model.ProjectCatalog;
import com.todod.model.ProjectFile;
import com.todod.model.ProjectInfo;
import com.todod.model.User;
import com.todod.service.IUserService;
import com.todod.service.ProjectCatalogService;
import com.todod.service.ProjectFileService;
import com.todod.service.ProjectInfoService;

/**
 * @author lql
 * @date 2020年12月25日
 */
@RestController
@RequestMapping("/projectCatalog")
public class ProjectCatalogController extends BaseManageController {

	@Autowired
	public ProjectCatalogService projectCatalogService;
	@Autowired
	public ProjectCatalogMapper projectCatalogMapper;
	@Autowired
	private ProjectFileService projectFileService;
	@Autowired
	public ProjectInfoService projectInfoService;
	@Autowired
	private IUserService iUserService;

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectFile
	 * @return
	 */
	@RequestMapping("/getTreeList")
	public ResultData getTreeList(@RequestAttribute Long _userId, Long xiangmuId) {
		User user = iUserService.getById(_userId);

		// 获取公共数据
		List<ProjectCatalog> list = projectCatalogService.getListByFlag("M", xiangmuId);
		ProjectCatalog projectCatalog1 = new ProjectCatalog();
		projectCatalog1.setId(1L);
		projectCatalog1.setParentId(-1L);
		projectCatalog1.setSortNum(0);
		projectCatalog1.setTypeName("通用目录");
		projectCatalog1.setFlag("M");
		projectCatalog1.setState("1");
		projectCatalog1.setXiangmuId(null);
		projectCatalog1.setDepartmentId(null);

		list.add(projectCatalog1);

		// 获取个人创建数据
		List<ProjectCatalog> plist = projectCatalogService.getListByParentId(user.getDepartid(), xiangmuId, _userId);
		ProjectCatalog projectCatalog = new ProjectCatalog();
		projectCatalog.setId(2L);
		projectCatalog.setParentId(-1L);
		projectCatalog.setSortNum(0);
		projectCatalog.setTypeName("个性目录");
		projectCatalog.setFlag("U");
		projectCatalog.setState("1");
		projectCatalog.setXiangmuId(xiangmuId);
		projectCatalog.setDepartmentId(user.getDepartid());

		plist.add(projectCatalog);

		if (plist != null && plist.size() > 0) {
			list.addAll(plist);
		}

		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqProjectCatalog
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody ProjectCatalog projectCatalog) {

		if (projectCatalog.getId() != null) {
			projectCatalogService.editData(_userId, projectCatalog);
		} else {
			projectCatalogService.saveData(_userId, projectCatalog);
		}
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqProjectCatalog
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody ProjectCatalog projectCatalog) {

		projectCatalogService.editData(_userId, projectCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultData delete(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		List<ProjectFile> list = projectFileService.getListByXftId(code);

		if (list != null && list.size() > 0) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "当前目录下已有文档，请先删除文档再删除目录");
		}

		projectCatalogMapper.deleteById(code);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}
		return ResultData.success("ok", projectCatalogService.getInfo(code));
	}

	/**
	 * @Title: getLeafNode
	 * @Description: 获取全部叶子节点
	 * @author dyl
	 * @date: 2021-01-28
	 * @return
	 * @return Result
	 */
	@GetMapping("/getAllLeafNode")
	public ResultData getAllLeafNode(@RequestAttribute Long _userId, Long xiangmuId) {
		User user = iUserService.getById(_userId);
		// 获取公共数据
		List<ProjectCatalog> list = projectCatalogService.getListByFlag("M", xiangmuId);

		// 获取个人创建数据
		List<ProjectCatalog> plist = projectCatalogService.getListByParentId(user.getDepartid(), xiangmuId, _userId);

		if (plist != null && plist.size() > 0) {
			list.addAll(plist);
		}
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectFile
	 * @return
	 */
	@RequestMapping("/getXMTreeList")
	public ResultData getXMTreeList(@RequestAttribute Long _userId) {
		User user = iUserService.getById(_userId);

		List<ProjectInfo> xmList = projectInfoService.getXMListByDpId(user.getDepartid());

		List<ProjectCatalog> list = new ArrayList<ProjectCatalog>();
		for (ProjectInfo projectInfo : xmList) {

			ProjectCatalog projectCatalog1 = new ProjectCatalog();
			projectCatalog1.setId(projectInfo.getId());
			projectCatalog1.setParentId(-1L);
			projectCatalog1.setSortNum(0);
			projectCatalog1.setTypeName(projectInfo.getXiangmumingcheng());
			projectCatalog1.setFlag(null);
			projectCatalog1.setState("1");
			projectCatalog1.setXiangmuId(null);
			projectCatalog1.setDepartmentId(null);

			list.add(projectCatalog1);

			// 获取公共数据
			List<ProjectCatalog> list1 = projectCatalogService.getListByFlag("M", projectInfo.getId());
			ProjectCatalog projectCatalog2 = new ProjectCatalog();
			projectCatalog2.setId(projectInfo.getId() + 1L);
			projectCatalog2.setParentId(projectInfo.getId());
			projectCatalog2.setSortNum(0);
			projectCatalog2.setTypeName("通用目录");
			projectCatalog2.setFlag("M");
			projectCatalog2.setState("1");
			projectCatalog2.setXiangmuId(null);
			projectCatalog2.setDepartmentId(null);

			list1.add(projectCatalog2);
			list.addAll(list1);

			// 获取个人创建数据
			List<ProjectCatalog> plist = projectCatalogService.getListByParentId(user.getDepartid(),
					projectInfo.getId(), _userId);
			ProjectCatalog projectCatalog3 = new ProjectCatalog();
			projectCatalog3.setId(projectInfo.getId() + 2L);
			projectCatalog3.setParentId(projectInfo.getId());
			projectCatalog3.setSortNum(0);
			projectCatalog3.setTypeName("个性目录");
			projectCatalog3.setFlag("U");
			projectCatalog3.setState("1");
			projectCatalog3.setXiangmuId(projectInfo.getId());
			projectCatalog3.setDepartmentId(user.getDepartid());

			plist.add(projectCatalog3);

			if (plist != null && plist.size() > 0) {
				list.addAll(plist);
			}
		}

		return ResultData.success("ok", list);
	}

}
